﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.SPP.EntidadesNegocio;
using MJM.CBD.Conn;

namespace MJM.SPP.AccesoDatos
{
    public class RestriccionModAD
    {
        public int Insertar(RestriccionModEN obj)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_i_T_WEB_RESEXT_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmID, prmTipo, prmAcceso;
            int codigo = 0;
            try
            {
                prmID = cmd.CreateParameter();
                prmID.Direction = ParameterDirection.ReturnValue;
                prmID.SqlDbType = SqlDbType.Int;
                cmd.Parameters.Add(prmID);

                prmTipo = cmd.CreateParameter();
                prmTipo.ParameterName = "@vRESEXT_chTIPRES";
                prmTipo.SqlDbType = SqlDbType.VarChar;
                prmTipo.Value = obj.RESEXT_chTIPRES;
                cmd.Parameters.Add(prmTipo);

                prmAcceso = cmd.CreateParameter();
                prmAcceso.ParameterName = "@vRESEXT_inACCRES";
                prmAcceso.SqlDbType = SqlDbType.Int;
                prmAcceso.Value = obj.RESEXT_inACCRES;
                cmd.Parameters.Add(prmAcceso);

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                codigo = Convert.ToInt32(prmID.Value);
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                codigo = 0;
                cmd.Dispose();
                conn.Dispose();
            }
            return codigo;
        }

        public int Eliminar(int Mod)
        {
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_u_T_WEB_RESEXT_Eliminar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prm;
            int codigo = 1;
            try
            {
                prm = cmd.CreateParameter();
                prm.ParameterName = "";
                prm.SqlDbType = SqlDbType.Int;
                prm.Value = Mod;
                cmd.Parameters.Add(prm);

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                codigo = 0;
                cmd.Dispose();
                conn.Dispose();
            }

            return codigo;
        }

        public RestriccionModEN Obtener(int Mod)
        {
            RestriccionModEN obj = null;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_WEB_RESEXT_Obtener", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prm;
            SqlDataReader dr;
            try
            {
                prm = cmd.CreateParameter();
                prm.ParameterName = "@vRESEXT_inACCRES";
                prm.SqlDbType = SqlDbType.Int;
                prm.Value = Mod;
                cmd.Parameters.Add(prm);
                cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    obj = new RestriccionModEN();
                    obj.RESEXT_P_inID = dr.GetInt32(0);
                    obj.RESEXT_chTIPRES = dr.GetString(1);
                    obj.RESEXT_inACCRES = dr.GetInt32(2);
                    obj.RESEXT_inFLAELI = dr.GetInt32(3);
                }
                cmd.Connection.Close();
                conn.Dispose();
                cmd.Dispose();
            }
            catch (Exception ex)
            {
                obj = null;
                conn.Dispose();
                cmd.Dispose();
            }
            return obj;
        }

        public List<RestriccionModEN> Listar()
        {
            List<RestriccionModEN> lst = new List<RestriccionModEN>();
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_WEB_RESEXT_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr;
            try
            {
                cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                RestriccionModEN obj;
                while (dr.Read())
                {
                    obj = new RestriccionModEN();
                    obj.RESEXT_P_inID = dr.GetInt32(0);
                    obj.RESEXT_chTIPRES = dr.GetString(1);
                    obj.RESEXT_inACCRES = dr.GetInt32(2);
                    obj.RESEXT_inFLAELI = dr.GetInt32(3);
                    obj.Nombre = dr.GetString(4);
                    lst.Add(obj);
                }
                cmd.Connection.Close();
                conn.Dispose();
                cmd.Dispose();
            }
            catch (Exception ex)
            {
                conn.Dispose();
                cmd.Dispose();
            }
            return lst;
        }
    }
}
